package cn.ickck.law_paper_final.controller;


import cn.ickck.law_paper_final.model.entity.Pa;
import cn.ickck.law_paper_final.model.entity.PaBatch;
import cn.ickck.law_paper_final.model.response.PaResponse;
import cn.ickck.law_paper_final.service.PaBatchService;
import cn.ickck.law_paper_final.service.PaService;
import cn.ickck.law_paper_final.utils.ExcelUtils;
import cn.ickck.law_paper_final.utils.JsonData;
import cn.ickck.law_paper_final.utils.PageHelperUtils;
import cn.ickck.law_paper_final.utils.RoundNoOfUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.Field;
import java.util.*;

/**
 * @Author: ck
 * @Description:
 * @Date: Create in 13:03 2020/12/13
 */
@RestController
@RequestMapping("/api/pub/v2/pa")
public class PaController {
    @Autowired
    private PaService paService;

    @Autowired
    private PaBatchService paBatchService;

    @Transactional
    @RequestMapping("insert")
    public JsonData insert(@RequestBody Pa pa)
    {

        if(pa.getBirthDate()==null)
        {
            pa.setBirthDate(new Date(2000,4,20));
        }
        if(pa.getContractDate()==null)
        {
            pa.setContractDate(new Date(2000,4,20));
        }
        if(pa.getCourtDate()==null)
        {
            pa.setCourtDate(new Date(2000,4,20));
        }
        if(pa.getDeadlineDate()==null)
        {
            pa.setDeadlineDate(new Date(2000,4,20));
        }
        if(pa.getExecuteDate()==null)
        {
            pa.setExecuteDate(new Date(2000,4,20));
        }
        if(pa.getExpiredDate()==null)
        {
            pa.setExpiredDate(new Date(2000,4,20));
        }
        if(pa.getLaunchedDate()==null)
        {
            pa.setLaunchedDate(new Date(2000,4,20));
        }
        if(pa.getLitigationDate()==null)
        {
            pa.setLitigationDate(new Date(2000,4,20));

        }
        if(pa.getStartDateInterestAfterJudgment()==null)
        {
            pa.setStartDateInterestAfterJudgment(new Date(2000,4,20));

        }
        if(pa.getFirstOverdueDate()==null)
        {
            pa.setFirstOverdueDate(new Date(2000,4,20));

        }
        if(pa.getLastPaidDate()==null)
        {
            pa.setLastPaidDate(new Date(2000,4,20));

        }
        //插入批次
        PaBatch paBatch = new PaBatch();
        paBatch.setName("eg:日期+导入者");
        String key = UUID.randomUUID().toString();
        paBatch.setDate(new Date());
        paBatch.setKey(key);
        pa.setKey(key);
        paBatchService.insert(paBatch);
        paService.insert(pa);
        return JsonData.buildSuccess("插入成功");
    }

    @RequestMapping("deleteById")
    public JsonData deleteById(Integer id)
    {
        paService.deleteById(id);
        return JsonData.buildSuccess("删除成功");
    }

    @RequestMapping("update")
    public JsonData update(@RequestBody Pa pa)
    {
        paService.update(pa);
        return JsonData.buildSuccess("更新成功");
    }

    @RequestMapping("selectByPage")
    public JsonData selectByPage(Integer page)
    {
        PaResponse paResponse = new PaResponse();
        int count = paService.count();
        int start = PageHelperUtils.getStart(count,page);
        int end = PageHelperUtils.getEnd(count,page);
        List<Pa> paList = paService.selectByPage(start,end);
        paResponse.setCount(count);
        paResponse.setPaList(paList);
        return JsonData.buildSuccess(paResponse);
    }

    @Transactional
    @RequestMapping("importExcel")
    public JsonData  importExcel(@RequestParam("file") MultipartFile file)throws Exception
    {
        List<Pa> paList = ExcelUtils.importExcel(file, 0, 1, Pa.class);
        PaBatch paBatch = new PaBatch();
        String key = UUID.randomUUID().toString();
        paBatch.setName("eg:日期+时间");
        paBatch.setKey(key);
        paBatch.setDate(new Date());
        paBatchService.insert(paBatch);
        for (Pa pa:paList)
        {


            Class cls = pa.getClass();
            Field[] fields = cls.getDeclaredFields();

            for (Field field:fields)
            {
                field.setAccessible(true);
                String type_name = field.getType().getSimpleName();

                if("String".equals(type_name))
                {

                    //test push

                    String str = (String) field.get(pa);


                    if(str!=null&&str.length()>15&&(str.contains("99999")|str.contains("00000")))
                    {
                        str = RoundNoOfUtil.RoundNoOf(str,2);
                    }
                    field.set(pa,str);

                }
                if("Date".equals(type_name))
                {
                    Date date = (Date)field.get(pa);
                    if(date==null)
                    {
                        field.set(pa,new Date(2000,4,20));

                    }

                }

            }

            pa.setKey(key);
            paService.insert(pa);
        }
        return JsonData.buildSuccess("批量录入成功");

    }









    @RequestMapping("selectByNameOrKey")
    public JsonData selectByNameOrKey(String key,int page)
    {
        PaResponse paResponse= new PaResponse();
        List<Pa> paList = paService.selectByNameOrKey(key);
        int start = PageHelperUtils.getStart(paList.size(),page);
        int end = PageHelperUtils.getEnd(paList.size(),page);
        List<Pa> paList1 = new ArrayList<>();
        for(int i =start;i<end;i++)
        {
            paList1.add(paList.get(i));
        }
        paResponse.setCount(paList.size());
        paResponse.setPaList(paList1);

        return JsonData.buildSuccess(paResponse);
    }



}
